<html>
<head><meta charset="utf-8"><title>Dealing with &amp;&#x27;static [&amp;&#x27;static str] values · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html">Dealing with &amp;&#x27;static [&amp;&#x27;static str] values</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219985725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219985725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maurizio De Santis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219985725">(Dec 15 2020 at 14:25)</a>:</h4>
<p>Hello everyone, Rust newbie here!</p>
<p>I'm stuck trying to use strum's <code>VARIANTS</code> value, whose type seems to be <code>&amp;'static [&amp;'static str]</code>. I have the following enum:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">serde</span>::<span class="p">{</span><span class="n">Serialize</span><span class="p">,</span><span class="w"> </span><span class="n">Deserialize</span><span class="p">};</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">strum_macros</span>::<span class="p">{</span><span class="n">EnumString</span><span class="p">,</span><span class="w"> </span><span class="n">EnumVariantNames</span><span class="p">};</span><span class="w"></span>

<span class="cp">#[derive(Serialize, Deserialize, Debug, EnumString, EnumVariantNames)]</span><span class="w"></span>
<span class="cp">#[serde(rename_all = </span><span class="s">"lowercase"</span><span class="cp">)]</span><span class="w"></span>
<span class="cp">#[strum(serialize_all = </span><span class="s">"lowercase"</span><span class="cp">)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">MovieId</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Chihiro</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Ged</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Kaguyahime</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Karigurashi</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Kazetachinu</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Kokurikozaka</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Marnie</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Ponyo</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The problem is that I can't see any useful methods on <code>MovieId::VARIANTS</code> (<code>map</code>, <code>to_iter</code>, ... ) <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> I'm sure I'm missing something obvious, maybe I should convert it to something else... any ideas?</p>



<a name="219986061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219986061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219986061">(Dec 15 2020 at 14:28)</a>:</h4>
<p>If it's a <code>&amp;'static [&amp;'static str]</code>, then you can do <code>for s in MovieId::VARIANTS { ... }</code> or <code>MovieId::VARIANTS.iter()</code> and so on to use it as an iterator</p>



<a name="219986745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219986745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219986745">(Dec 15 2020 at 14:32)</a>:</h4>
<p>See <a href="https://doc.rust-lang.org/std/primitive.slice.html">https://doc.rust-lang.org/std/primitive.slice.html</a> for all the methods that can be called on a slice</p>



<a name="219987056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219987056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maurizio De Santis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219987056">(Dec 15 2020 at 14:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> thank you, it works! May I ask you where is the documentation of that <code>.iter</code> method? Is this one: <a href="https://doc.rust-lang.org/std/primitive.slice.html#method.iter">https://doc.rust-lang.org/std/primitive.slice.html#method.iter</a> ?</p>
<p>I mean, with what kind of value am I dealing here? With an array slice?</p>



<a name="219987188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219987188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219987188">(Dec 15 2020 at 14:35)</a>:</h4>
<p>It's a <code>&amp;[&amp;str]</code>, which is a pointer to a slice of <code>&amp;str</code>s. Yes that's the same <code>iter()</code></p>



<a name="219987348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219987348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219987348">(Dec 15 2020 at 14:36)</a>:</h4>
<p>Concretely, this is a pair of a length and a pointer to an array of that length, each member of which is a <code>&amp;str</code>, which is itself a pair of a length and a pointer to a UTF-8 string of that size</p>



<a name="219987519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219987519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219987519">(Dec 15 2020 at 14:37)</a>:</h4>
<p>this is the same as any other array slice, except this one has <code>'static</code> lifetime, meaning it's baked into your binary's read only section and is always available</p>



<a name="219987742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dealing%20with%20%26%27static%20%5B%26%27static%20str%5D%20values/near/219987742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Maurizio De Santis <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dealing.20with.20.26&#x27;static.20.5B.26&#x27;static.20str.5D.20values.html#219987742">(Dec 15 2020 at 14:39)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span>now it makes perfectly sense <span aria-label="grinning face with smiling eyes" class="emoji emoji-1f601" role="img" title="grinning face with smiling eyes">:grinning_face_with_smiling_eyes:</span> thank you again!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>